<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
	<title>Document</title>
</head>
<body id="body">
		<ul id="list">
			<li class="on">1</li>
			<li class="select">2</li>
			<li class="on">3</li>
			<li>4</li>
		</ul>
		<p class="on">class为on的p标签</p>
</body>
</html>
<script type="text/javascript">
  window.onload=function(){
    // var body=document.getElementsByTagName("body");
    // alert(body);
    var len1= getByClass('list','on');
    alert(len1.length);  // 结果等于2为正确
    var len2= getByClass('on');
    alert(len2.length);  // 结果等于3为正确
}
function getByClass(parent,clsName){
 //定义函数getByClass()实现获取document或指定父元素下所有class为on的元素  
    var boxArr=new Array();
    // var oParent=document.getElementById(parent)||document.getElementById("body");
    var oParent=document.getElementById(parent);
    // var oParent=document.getElementsByTagName(parent);
    console.log(typeof (oParent) );
    var boxs=oParent.getElementsByTagName("*")||oParent[0].getElementsByTagName("*");
    // var boxs=oParent.getElementsByClassName("clsName");
    for(var i=0;i<boxs.length;i++){
        if(boxs[i].className==clsName){
            boxArr.push(boxs[i]);
        }
    }
    return boxArr;
}
</script>